
package org.parademo.web.model;

import org.apache.commons.lang.StringUtils;
import org.parademo.model.AddressEntity;
import org.parademo.model.UserEntity;
import org.parademo.web.controller.util.ViewFormatterUtil;
import org.springframework.validation.BindingResult;

public class UserProfileView {

    private Long userId;
    private String userName;
    private String firstName;
    private String lastName;
    private String birthDate;
    private String joinDate;

    private Long addressId;
    private String addressStreetName;
    private String addressHouseNumber;
    private String addressZipCode;
    private String addressCity;
    private String addressCountryId;

    public UserProfileView() { }

    public UserProfileView(UserEntity entity) {

        this.userId = entity.getId();
        this.userName = entity.getUserName();
        this.firstName = entity.getFirstName();
        this.lastName = entity.getLastName();
        this.birthDate = ViewFormatterUtil.formatDateToString(entity.getBirthDate());
        this.joinDate = ViewFormatterUtil.formatDateToString(entity.getJoinDate());

        AddressEntity address = entity.getAddress();
        this.addressId = address.getId();
        this.addressStreetName = address.getStreetName();
        this.addressHouseNumber = address.getHouseNumber();
        this.addressCity = address.getCity();
        this.addressZipCode = address.getZipCode();

    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getJoinDate() {
        return joinDate;
    }

    public void setJoinDate(String joinDate) {
        this.joinDate = joinDate;
    }

    public String getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }

    public Long getAddressId() {
        return addressId;
    }

    public void setAddressId(Long addressId) {
        this.addressId = addressId;
    }

    public String getAddressStreetName() {
        return addressStreetName;
    }

    public void setAddressStreetName(String addressStreetName) {
        this.addressStreetName = addressStreetName;
    }

    public String getAddressHouseNumber() {
        return addressHouseNumber;
    }

    public void setAddressHouseNumber(String addressHouseNumber) {
        this.addressHouseNumber = addressHouseNumber;
    }

    public String getAddressZipCode() {
        return addressZipCode;
    }

    public void setAddressZipCode(String addressZipCode) {
        this.addressZipCode = addressZipCode;
    }

    public String getAddressCity() {
        return addressCity;
    }

    public void setAddressCity(String addressCity) {
        this.addressCity = addressCity;
    }

    public String getAddressCountryId() {
        return addressCountryId;
    }

    public void setAddressCountryId(String addressCountryId) {
        this.addressCountryId = addressCountryId;
    }

    public void validateUsing(BindingResult bindingResult) {
        if (StringUtils.isEmpty(firstName)) {
            bindingResult.rejectValue("firstName", "profile.edit.validation.firstName.required");
        }
        if (StringUtils.isEmpty(lastName)) {
            bindingResult.rejectValue("lastName", "profile.edit.validation.lastName.required");
        }
        if (birthDate == null) {
            bindingResult.rejectValue("birthDate", "profile.edit.validation.birthDate.required");
        }
        if (joinDate == null) {
            bindingResult.rejectValue("joinDate", "profile.edit.validation.joinDate.required");
        }
        if (StringUtils.isEmpty(addressStreetName)) {
            bindingResult.rejectValue("addressStreetName", "profile.edit.validation.addressStreetName.required");
        }
        if (StringUtils.isEmpty(addressHouseNumber)) {
            bindingResult.rejectValue("addressStreetName", "profile.edit.validation.addressHouseNumber.required");
        }
        if (StringUtils.isEmpty(addressCity)) {
            bindingResult.rejectValue("addressCity", "profile.edit.validation.addressCity.required");
        }
        if (StringUtils.isEmpty(addressZipCode)) {
            bindingResult.rejectValue("addressZipCode", "profile.edit.validation.addressZipCode.required");
        }
        if (StringUtils.isEmpty(addressCountryId)) {
            bindingResult.rejectValue("addressCountryId", "profile.edit.validation.addressCountryId.required");
        }
    }
}
